package io.jee.caisan.weixin.config.web;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import io.jee.caisan.weixin.config.web.interceptor.WxmpOpenidInterceptor;
import io.jee.caisan.weixin.config.web.interceptor.WxmpUserInterceptor;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebMvcConfiguration implements WebMvcConfigurer {
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		String[] excludeOpenid = {"/api/**", "/error", "/index", "/admin/**", "/application/**", "/wechat/**", "/pay/notify"};
		registry.addInterceptor(WxmpOpenidInterceptor()).addPathPatterns("/**").excludePathPatterns(excludeOpenid);
		String[] excludeUser = ArrayUtils.addAll(excludeOpenid, new String[]{"/user/login", "/user/forgot", "/user/register"});
		registry.addInterceptor(WxmpUserInterceptor()).addPathPatterns("/**").excludePathPatterns(excludeUser);
	}
	
	@Bean
	public HandlerInterceptor WxmpOpenidInterceptor(){
		return new WxmpOpenidInterceptor();
	}
	
	@Bean
	public HandlerInterceptor WxmpUserInterceptor(){
		return new WxmpUserInterceptor();
	}
	
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

}